package com.hnucm.c202001020216.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.hnucm.c202001020216.DataApplication;
import com.hnucm.c202001020216.R;
import com.hnucm.c202001020216.api.RegisterApi;
import com.hnucm.c202001020216.databinding.ActivityRegisterBinding;
import com.hnucm.c202001020216.pojo.Response;
import com.hnucm.c202001020216.pojo.User;
import com.hnucm.c202001020216.utils.RetrofitUtils;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;

public class RegisterActivity extends AppCompatActivity {

    private final static String reg="^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";
    ActivityRegisterBinding binding;
    User user=new User();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= DataBindingUtil.setContentView(RegisterActivity.this, R.layout.activity_register);


        //设置沉浸式布局
        ImmersionBar.with(this)
                .transparentStatusBar()
                .statusBarDarkFont(true)
                .init();

        binding.registerBtnCon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user.mobile=binding.phone.getText().toString();
                user.password=binding.password.getText().toString();
                if(!user.mobile.equals("")&&!user.password.equals("")&&user.mobile.matches(reg)){
                    //网络请求提交注册信息
                    String dataBody=new Gson().toJson(user);
                    RegisterApi api= RetrofitUtils.getRetrofit(DataApplication.HttpURL).create(RegisterApi.class);
                    RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),dataBody);
                    Call<Response> call=api.userRegister(body);
                    call.enqueue(new Callback<Response>() {
                        @Override
                        public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
                            if(response.body().msg.equals("success")){
                                Toast.makeText(RegisterActivity.this,"注册成功，欢迎您，新用户。",Toast.LENGTH_SHORT).show();
                                finish();
                            }else{
                                Toast.makeText(RegisterActivity.this,"亲，账号可能已被注册哦",Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Response> call, Throwable t) {

                        }
                    });
                }else{
                    Toast.makeText(RegisterActivity.this,"亲，账号密码不能为空且电话格式要正确哦",Toast.LENGTH_SHORT).show();
                }
            }

        });
    }
}